<?php

namespace App\Http\Controllers\Api;

use App\Api\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Models\MUser;
use App\Models\MUserCash;

use App\Services\OSS;
use Illuminate\Http\Request;

class UploadController extends ApiController
{
    /**
     * UserController constructor.
     * @param Request $request
     */
    public  function upload(Request $request){
        $file = $request->file('file');

        //获取上传图片的临时地址 //"C:\Windows\php9F19.tmp
        $tmppath = $file->getRealPath();

        //生成文件名--图片名称
        $fileName = str_random(5) . $file->getFilename() . time().'.'.$file->getClientOriginalExtension();

        $pathName=date('Y/m/d').'/'.$fileName;//拼接上传的文件夹路径(按照日期格式1810/17/xxxx.jpg)
        //上传图片到阿里云OSS

        OSS::publicUpload(config('oss.BucketName'), $pathName, $tmppath, ['ContentType'=> $file->getClientMimeType()]);

       //获取上传图片的Url链接
        $this->data= OSS::getPublicObjectURL(config('oss.BucketName'), $pathName);
        return $this->ReturnMessage(200,getMessage(),$this->data);
//        echo OSS::getUrl('某个文件的名称'); // 打印出某个文件的外网链接
    }



}